package lsproduct

import (
	"fmt"
	"regexp"
	"strconv"
)

const testVersion = 5

var regHasLetters = regexp.MustCompile(`[0-9]`)

type LsproductError struct{}

func (e *LsproductError) Error() string {
	return fmt.Sprintf("error")
}

func LargestSeriesProduct(digits string, span int) (product int, err error) {
	if len(digits) < span {
		err = &LsproductError{}
		return
	}

	letters := regHasLetters.FindAllString(digits, -1)
	if len(letters) != len(digits) {
		err = &LsproductError{}
		return
	}

	if span < 0 {
		err = &LsproductError{}
		return
	}

	s := make([]int, len(digits)-span+1)
	for i := 0; i < len(s); i++ {
		s[i] = 1
		for j := 0; j < span; j++ {
			_1, _ := strconv.Atoi(letters[i+j])
			s[i] = s[i] * _1
		}
	}

	product = s[0]
	for i := 1; i < len(s); i++ {
		if product < s[i] {
			product = s[i]
		}
	}

	return
}
